#!/usr/bin/env node
//修复globalThis在node v10和v11上不支持的问题 https://github.com/jsdom/jsdom/issues/2795#issuecomment-711453386
global.globalThis = global;
const { red, yellow, white } = require("chalk");
const { ensureDir, writeFile, pathExists } = require("fs-extra");
const ora = require("ora");
const { dirname, resolve } = require("path")
const { getGameConfigTypeScript, getGameConfigJson, checkHelp, checkVersion } = require('./index');

async function run() {
    const t = Date.now();
    checkVersion();
    checkHelp();
    const input = "./gameConfig.xlsx";
    if (!await pathExists(input)) return ora().fail(red(`操作失败，配置表不存在：${yellow(resolve(input))}`));

    //ps路径
    let outputJson = "./resource/config/gameConfig.json";
    let outputTs = "./TypeScripts/GAME_CFG.ts";
    const isPS = await pathExists(outputTs);
    if (!isPS) {
        //考拉3d路径
        outputJson = "./bin/resource/config/gameConfig.json";
        outputTs = "./src/GAME_CFG.ts";
    }
    outputJson = resolve(outputJson);
    outputTs = resolve(outputTs);
    const gameConfigJson = getGameConfigJson(input);
    const gameConfigts = getGameConfigTypeScript(gameConfigJson);
    await Promise.all([
        ensureDir(dirname(outputJson)),
        ensureDir(dirname(outputTs))
    ]);
    await Promise.all([
        writeFile(outputJson, JSON.stringify(gameConfigJson, void 0, 4)),
        writeFile(outputTs, gameConfigts)
    ]);
    ora().succeed(yellow(`操作成功！\n当前为${white(isPS ? "PlaySmart" : "考拉3D")}项目！\n成功导表: \n${white(outputJson)}\n${white(outputTs)}\n耗时：${white(Date.now() - t)} ms`));
};


run();
